/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function trackChanges(formname, selector) {
    alert(formname);
    alert(selector);
    $(formname).trackChanges({
        events: "change blur keypress keydown click",
        changeListVisible: false
    });

    $(selector).each(function() {
        $(this).change(function(){
            if($('#TrackList')[0].length == 0) {
                $('#submit-id').attr('disabled', 'disabled')
            } else {
                $('#submit-id').removeAttr('disabled')
            }
        });
    });
}

/**
 * clear form fields value
 * ref: http://www.learningjquery.com/2007/08/clearing-form-data
 */
$.fn.clearForm = function() {
    return this.each(function() {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form')
            return $(':input',this).clearForm();
        if (type == 'text' || type == 'password' || tag == 'textarea' || type == 'hidden')
            this.value = '';
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
        else if (tag == 'select')
            this.selectedIndex = -1;
    });
};

$.fn.buildOptions = function(data, selectedValue) {
    for(i=0; i<data.length; i++){
        var option;
        if (data[i].label == selectedValue) {
            option = new Option(data[i].label, data[i].value, true);
        }
        else
            option = new Option(data[i].label, data[i].value);
        
        if ($.browser.msie) {
            this[0].add(option);
        } else {
            this[0].add(option, null);
        }
    };
}

$.fn.clearOptions = function() {
    while(this[0].length > 0)
        this[0].remove(this[0].length-1);
}

$.fn.exists = function() {
    return $(this).length > 0;
}